﻿using System;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using FireFly;

namespace XAMLStylePad.Models
{
    /// <summary>
    /// XAMLStylePad project file
    /// </summary>
    [Serializable]
    public class AppProject : ProjectBase
    {
        public override bool IsNew
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Open project
        /// </summary>
        /// <param name="path">project file path</param>
        public static AppProject Load(string path)
        {
            try
            {
                XmlSerializer xml = new XmlSerializer(typeof(AppProject));
                var prj = (AppProject)xml.Deserialize(File.OpenText(path));
                prj.Directory = path;
                prj.Name = System.IO.Path.GetFileName(path);
                return prj;
            }
            catch(Exception err)
            {
                ViewModelService.Instance.ProcessException(err);
                return null;
            }
        }

        public override void Save(string path)
        {
            try
            {
                //XmlSerializer xml = new XmlSerializer(typeof(AppProject));
                //xml.Serialize(File.CreateText(path), this);
                var formatter = new BinaryFormatter();
                using(var file = File.Create(path))
                {
                    formatter.Serialize(file, this.Sources);
                }
            }
            catch(Exception err)
            {
                ViewModelService.Instance.ProcessException(err);
            }
        }
        
    }
}
